/**
 * @Description: 响应拦截器
 * @author zhaojiuyi
 * @date 2023/4/15
 */
import {AxiosResponse} from "axios";
import {ApiResponse} from "../types";
import {ApiConstants} from "../../constants";
import {navigation} from "@/common/components/Navigation";
import store from "@/redux/store";
import {logoutAction} from "@/redux/actions/user.action";

export const responseInterceptor = (response: AxiosResponse<ApiResponse<any>>) => {
    const {code,msg} = response.data
    /* 处理没有token的问题 */
    if (code === ApiConstants.UNAUTHORIZED){
        store.dispatch(logoutAction())
        navigation("/login")
    }
    /* 有时候后端响应的msg可能是一个数组，只取第一个进行提示即可 */
    response.data.msg = typeof msg === "string" ? msg : msg[0]
    return response

}
